home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / DirectX / dxsdk_oct2004.exe / dxsdk.exe / Samples / C++ / Direct3D / MeshFromOBJ / MeshFromObj.fx < prev    next >
Encoding:
Text File  |  2004-09-27  |  5.0 KB  |  146 lines

  1. //--------------------------------------------------------------------------------------
  2. // File: MeshFromOBJ.fx
  3. //
  4. // The effect file for the MeshFromOBJ sample.  
  5. // 
  6. // Copyright (c) Microsoft Corporation. All rights reserved.
  7. //--------------------------------------------------------------------------------------
  8.  
  9.  
  10. //--------------------------------------------------------------------------------------
  11. // Global variables
  12. //--------------------------------------------------------------------------------------
  13. float3 g_vMaterialAmbient : Ambient = float3( 0.2f, 0.2f, 0.2f );   // Material's ambient color
  14. float3 g_vMaterialDiffuse : Diffuse = float3( 0.8f, 0.8f, 0.8f );   // Material's diffuse color
  15. float3 g_vMaterialSpecular : Specular = float3( 1.0f, 1.0f, 1.0f );  // Material's specular color
  16. float  g_fMaterialAlpha : Opacity = 1.0f;
  17. int    g_nMaterialShininess : SpecularPower = 32;
  18.  
  19. float3 g_vLightColor : LightColor = float3( 1.0f, 1.0f, 1.0f );        // Light color
  20. float3 g_vLightPosition : LightPosition = float3( 50.0f, 10.0f, 0.0f );   // Light position
  21. float3 g_vCameraPosition : CameraPosition;
  22.  
  23. texture  g_MeshTexture : Texture;   // Color texture for mesh
  24.             
  25. float    g_fTime : Time;            // App's time in seconds
  26. float4x4 g_mWorld : World;          // World matrix
  27. float4x4 g_mWorldViewProjection : WorldViewProjection; // World * View * Projection matrix
  28.  
  29.  
  30.  
  31. //--------------------------------------------------------------------------------------
  32. // Texture samplers
  33. //--------------------------------------------------------------------------------------
  34. sampler MeshTextureSampler = 
  35. sampler_state
  36. {
  37.     Texture = <g_MeshTexture>;    
  38.     MipFilter = LINEAR;
  39.     MinFilter = LINEAR;
  40.     MagFilter = LINEAR;
  41. };
  42.  
  43.  
  44. //--------------------------------------------------------------------------------------
  45. // Name: Projection
  46. // Type: Vertex Shader Fragment
  47. // Desc: Projection transform
  48. //--------------------------------------------------------------------------------------
  49. void Projection( float4 vPosObject: POSITION,
  50.                  float3 vNormalObject: NORMAL,
  51.                  float2 vTexCoordIn: TEXCOORD0,
  52.                  out float4 vPosProj: POSITION,
  53.                  out float2 vTexCoordOut: TEXCOORD0,
  54.                  out float4 vColorOut: COLOR0,
  55.                  uniform bool bSpecular
  56.                 )
  57. {
  58.     // Transform the position into world space for lighting, and projected space
  59.     // for display
  60.     float4 vPosWorld = mul( vPosObject, g_mWorld );
  61.     vPosProj = mul( vPosObject, g_mWorldViewProjection );
  62.     
  63.     // Transform the normal into world space for lighting
  64.     float3 vNormalWorld = mul( vNormalObject, (float3x3)g_mWorld );
  65.     
  66.     // Pass the texture coordinate
  67.     vTexCoordOut = vTexCoordIn;
  68.     
  69.     // Compute the light vector
  70.     float3 vLight = normalize( g_vLightPosition - vPosWorld.xyz );
  71.     
  72.     // Compute the ambient and diffuse components of illumination
  73.     vColorOut.rgb = g_vLightColor * g_vMaterialAmbient;
  74.     vColorOut.rgb += g_vLightColor * g_vMaterialDiffuse * saturate( dot( vLight, vNormalWorld ) );
  75.     
  76.     // If enabled, calculate the specular term
  77.     if( bSpecular )
  78.     {
  79.         float3 vCamera = normalize(vPosWorld.xyz - g_vCameraPosition);
  80.         float3 vReflection = reflect( vLight, vNormalWorld );
  81.         float  fPhongValue = saturate( dot( vReflection, vCamera ) );
  82.  
  83.         vColorOut.rgb += g_vMaterialSpecular * pow(fPhongValue, g_nMaterialShininess);
  84.     }
  85.         
  86.     vColorOut.a = g_fMaterialAlpha;
  87. }
  88.  
  89.  
  90.  
  91. //--------------------------------------------------------------------------------------
  92. // Name: Lighting
  93. // Type: Pixel Shader
  94. // Desc: Compute lighting and modulate the texture
  95. //--------------------------------------------------------------------------------------
  96. void Lighting( float2 vTexCoord: TEXCOORD0,
  97.                float4 vColorIn: COLOR0,
  98.                out float4 vColorOut: COLOR0,
  99.                uniform bool bTexture )
  100. {  
  101.     vColorOut = vColorIn;
  102.     
  103.     // Sample and modulate the texture
  104.     if( bTexture )
  105.         vColorOut.rgb *= tex2D( MeshTextureSampler, vTexCoord );
  106. }
  107.  
  108.  
  109. //--------------------------------------------------------------------------------------
  110. // Techniques
  111. //--------------------------------------------------------------------------------------
  112. technique Specular
  113. {
  114.     pass P0
  115.     {
  116.         VertexShader = compile vs_1_1 Projection(true);    
  117.         PixelShader = compile ps_1_4 Lighting(false);    
  118.     }
  119. }
  120.  
  121. technique NoSpecular
  122. {
  123.     pass P0
  124.     {
  125.         VertexShader = compile vs_1_1 Projection(false);    
  126.         PixelShader = compile ps_1_4 Lighting(false);    
  127.     }
  128. }
  129.  
  130. technique TexturedSpecular
  131. {
  132.     pass P0
  133.     {
  134.         VertexShader = compile vs_1_1 Projection(true);    
  135.         PixelShader = compile ps_1_4 Lighting(true);    
  136.     }
  137. }
  138.  
  139. technique TexturedNoSpecular
  140. {
  141.     pass P0
  142.     {
  143.         VertexShader = compile vs_1_1 Projection(false);    
  144.         PixelShader = compile ps_1_4 Lighting(true);    
  145.     }
  146. }